#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <error.h>
#include "head.h"
int getini(void)
{
    FILE *fp;
    int i = 0;
    char config[1024];
    char filesendnumber[10];
    char prefix[2];
    char path[] = "[path]";
    char line[] = "[line]";
    char ip[] = "[ip]";
    char port[] = "[port]";
    char mailfrom[] = "[mailfrom]";
    char server_port_str[6];
    int configlen;
    if((fp = fopen("mailfile.ini", "r")) == NULL)
    {
        perror("open mailfile.ini error");
        exit(1);
    }

    while((fgets(config, MAX_INI_RECORD_NUM, fp)) != NULL)
    {
        configlen = strlen(config);
        config[configlen-1] = '\0';
        strncpy(prefix, config, 1);
        if (strcmp(prefix, "#") == 0)
        {
            continue;
        }
        else if (strcmp(config, path) == 0)
        {
            fgets(file[i], MAX_INI_RECORD_NUM, fp);
            i++;
        }
        else if (strcmp(config, line) == 0)
        {
            fgets(filesendnumber, MAX_INI_RECORD_NUM, fp);
            sendnumber = atoi(filesendnumber);
        }
        else if (strcmp(ip, config) == 0)
        {
            fgets(server_ip, 16, fp);
            server_ip[strlen(server_ip) - 1] = '\0';
        }
        else if (strcmp(port, config) == 0)
        {
            fgets(server_port_str, 5, fp);
            server_port = atoi(server_port_str);
        }
        else if (strcmp(mailfrom, config) == 0)
        {
            fgets(mailfrom, 100, fp);
        }
    }
    fclose(fp);
    return i;
}
